// 用户侧相关API
import { get, post, put, postNormal } from '../common/ajax';
import { USR_USER_URL, USR_ACTIVITY_URL, USR_DISTRIBUTE_URL, USR_ENDORSEMENT_URL } from '../constants/url';
import { updateLocalUserInfo } from '../biztools/wex';
import { dealActivityListData, dealActivityDetailData } from '../biztools/activity';
import { dealAccountLogListData, dealWithdrawLogListData } from '../biztools/record';
import { dealEndorsementListData } from '../biztools/user';

/**
 * 识别用户
 * @param {String} code
 * @param {String} openId
 * @param {Number|String} b 代表信息分享人id
 * @param {Number|String} a 代表建立follow关系的用户
 */
export const userIdentify = async (params) => {
    const res = await postNormal(USR_USER_URL.userIdentify, params);
    return res.data || {};
};

/**
 * 发送验证码
 * @param {String}} mobile 
 */
export const sendVerifyCode = async (mobile) => {
    const res = await post(USR_USER_URL.verifyCode, { mobile });
    return res.data ? true : false;
};

/**
 * 绑定手机号
 * @param {String} mobile 手机号
 * @param {String} code 验证码
 */
export const bindMobile = async (params) => {
    const res = await put(USR_USER_URL.bindMobile, params);
    updateLocalUserInfo(res.data);
    return res.data || false;
};

/**
 * 绑定手机号
 * @param {String} iv
 * @param {String} code
 * @param {String} encryptedData加密数据
 */
export const bindMobileEncrypted = async (params) => {
    const res = await put(USR_USER_URL.bindMobileEncrypted, params);
    updateLocalUserInfo(res.data);
    return res.data || false;
};

/**
 * 修改用户信息
 * @param {String} homepageBg 背景图
 * @param {String} nickName 昵称
 * @param {String} intro 简介
 * @param {String} pic 头像
 */
export const updateUserInfo = async (params) => {
    const res = await put(USR_USER_URL.updateInfo, params);
    // 更新本地保存的用户信息
    updateLocalUserInfo(res.data);
    return res.data ? true : false;
};

/**
 * 获取用户信息(分场景)
 * @param {String} scene 场景 user-homepage | 
 */
export const getUserExtInfo = async (scene) => {
    const res = await get(USR_USER_URL.userInfo, { page: scene });
    return res.data || {};
};

/**
 * 获取用户收益信息
 */
export const getUserEarningInfo = async () => {
    const res = await get(USR_USER_URL.userAccount);
    return res.data || {};
};

/**
 * 用户申请提现
 * @param {Object} params 参数
 */
export const userApplyWithdraw = async (params) => {
    const res = await post(USR_USER_URL.userWithdraw, { ...params });
    return res.data && res.success ? true : false;
};

/**
 * 获取用户提现记录
 */
export const getUserWithdrawLog = async () => {
    const res = await get(USR_USER_URL.userWithdrawLog);
    return res.data ? dealWithdrawLogListData(res.data.rows, 'user') : [];
};

/**
 * 获取收益明细
 * @param {Object} params 列表获取参数
 */
export const getUserEarningLogList = async (params) => {
    const res = await get(USR_USER_URL.userAccountLog, { ...params });
    return res.data ? dealAccountLogListData(res.data.rows) : [];
};

/**
 * 获取收益统计
 * @param {Object} params 参数 
 */
export const getUserEarningStatistics = async (params) => {
    const res = await get(USR_USER_URL.userStatistics, { ...params });
    return res.data || {};
};

/**
 * 获取用户关注(用户)列表
 */
export const getUserFollow = async (params) => {
    const res = await get(USR_USER_URL.userFollow, params);
    return res.data ? res.data.rows : [];
};

/**
 * 获取特定用户的关注(用户)列表
 * @param {String|Number} userId 用户id
 */
export const getSpecialUserFollow = async (userId) => {
    const res = await get(USR_USER_URL.specialUserFollow(userId));
    return res.data ? res.data.rows : [];
};

/**
 * 取消关注用户
 * @param {Number|String} userId 用户id
 */
export const cancelFollowUser = async (userId) => {};

/**
 * 获取用户关注(品牌)列表
 */
export const getUserFollowBrand = async (params) => {
    const res = await get(USR_USER_URL.userFollowBrand, params);
    return res.data ? res.data.rows : [];
};

/**
 * 获取特定用户的关注(品牌)列表
 * @param {String|Number} userId 用户id
 */
export const getSpecialUserFollowBrand = async (userId) => {
    const res = await get(USR_USER_URL.specialUserFollowBrand(userId));
    return res.data ? res.data.rows : [];
};

/**
 * 取消关注品牌
 * @param {Number|String} brandId 品牌id
 */
export const cancelFollowBrand = async (brandId) => {};

/**
 * 获取用户关注(店铺)列表
 */
 export const getUserFollowStore = async (params) => {
    const res = await get(USR_USER_URL.userFollowStore, params);
    return res.data ? res.data.rows : [];
};

/**
 * 获取特定用户的关注(店铺)列表
 * @param {String|Number} userId 用户id
 */
export const getSpecialUserFollowStore = async (userId) => {
    const res = await get(USR_USER_URL.specialUserFollowStore(userId));
    return res.data ? res.data.rows : [];
};

/**
 * 取消关注店铺
 * @param {Number|String} storeId 店铺id
 */
export const cancelFollowStore = async (storeId) => {};

/**
 * 获取自己的粉丝列表
 * @param {Object} params 参数
 */
export const getUserFansList = async (params) => {
    const res = await get(USR_USER_URL.userFans, params);
    return res.data ? res.data.rows : [];
};

/**
 * 获取分享用户的粉丝列表
 * @param {String|Number} userId 用户id
 */
export const getSpecialUserFansList = async (userId) => {
    const res = await get(USR_USER_URL.specialUserFans(userId));
    return res.data || [];
};

/**
 * 备注粉丝名称
 * @param {Number|String} fansId 粉丝id
 * @param {String} aliasName 备注名
 */
export const remarkFansName = async (fansId, aliasName='') => {
    const res = await put(USR_USER_URL.remarkFans(fansId), {
        aliasName,
    });
    return res.data ? true : false;
};

// 品牌
/**
 * 获取代言的品牌列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getAdvocacyBrandList = async () => {
    const params = { pageNo: 1, pageSize: 100 };
    const res = await get(USR_ENDORSEMENT_URL.brandList, params);
    return res.data ? dealEndorsementListData(res.data.rows, 'brand') : [];
};

// 店铺
/**
 * 获取代言的店铺列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
export const getAdvocacyStoreList = async () => {
    const params = { pageNo: 1, pageSize: 100 };
    const res = await get(USR_ENDORSEMENT_URL.storeList, params);
    return res.data ? dealEndorsementListData(res.data.rows, 'store') : [];
};

// 分销
/**
 * 获取分销设置信息、申请信息
 * @param {Number|String} brandId 品牌Id
 */
export const getDistributeRequest = async (brandId) => {
    const res = await get(USR_DISTRIBUTE_URL.settingRequest, { brandId });
    return res.data || {};
};

/**
 * 用户申请成为分销员
 * @param {Number|String} brandId 品牌Id 
 * @param {String} applyFields 申请信息JSON字符串 
 */
export const saveDistributeRequest = async (params) => {
    const res = await post(USR_DISTRIBUTE_URL.request, params);
    return res.data || false;
};

// 代言
/**
 * 获取品牌代言设置信息、申请信息
 * @param {Number|String} brandId 品牌Id
 */
export const getEndorsementBrandRequest = async (brandId) => {
    const res = await get(USR_ENDORSEMENT_URL.settingBrandRequest, { brandId });
    return res.data || {};
};

/**
 * 用户申请成为品牌代言人
 * @param {Number|String} brandId 品牌Id
 * @param {String} applyFields 申请信息JSON字符串 
 */
export const saveEndorsementBrandRequest = async (params) => {
    const res = await post(USR_ENDORSEMENT_URL.brandRequest, params);
    return res.data || false;
};

/**
 * 获取店铺代言设置信息、申请信息
 * @param {Number|String} storeId 店铺Id
 */
export const getEndorsementStoreRequest = async (storeId) => {
    const res = await get(USR_ENDORSEMENT_URL.settingStoreRequest, { storeId });
    return res.data || {};
};

/**
 * 用户申请成为店铺代言人
 * @param {Number|String} storeId 店铺Id
 * @param {String} applyFields 申请信息JSON字符串 
 */
export const saveEndorsementStoreRequest = async (params) => {
    const res = await post(USR_ENDORSEMENT_URL.storeRequest, params);
    return res.data || false;
};

// 活动
/**
 * 获取首页最近活动
 * @param {Object} params 参数
 */
export const getIndexRecentActivity = async (params) => {
    const res = await get(USR_ACTIVITY_URL.recentActivityList, params)
    return res.data ? dealActivityListData(res.data.rows) : [];
};

/**
 * 获取用户参与的活动列表
 * @param {Object} params 参数
 */
export const getUserJoinActivityList = async (params) => {
    const res = await get(USR_ACTIVITY_URL.joinActivityList, params);
    return res.data ? dealActivityListData(res.data.rows) : [];
};

/**
 * 获取代言的活动列表
 * @param {Object} params 参数
 * @param {Object} ownerInfo
 */
export const getAdvocacyActivityList = async (params, ownerInfo) => {
    const res = await get(USR_ACTIVITY_URL.endorsedActivityList, params);
    return res.data && res.success ? dealActivityListData(res.data.rows, ownerInfo) : [];
};

/**
 * 获取活动信息
 * @param {Number|String} activityId 
 */
export const getActivityInfo = async (activityId) => {
    const res = await get(USR_ACTIVITY_URL.activityInfo(activityId));
    return res.data ? dealActivityDetailData(res.data) : {};
};

/**
 * 取消关注活动
 * @param {Number|String} activityId 用户id
 */
export const cancelFollowActivity = async (activityId) => {};

/**
 * 获取用户代言的活动
 * @param {String|Number} userId 用户id
 */
export const getUserEndorsementActivityList = async (userId, params) => {
    const res = await get(USR_ACTIVITY_URL.userEndorsementActivityList(userId), { ...params });
    const { rows = [] } = res.data || {};
    return dealActivityListData(rows, {});
};

/**
 * 获取用户实名信息
 */
export const getUserPrivacyInfo = async () => {
    const res = await get(USR_USER_URL.userPrivacy);
    return res.data || {};
};